package dav.mobile.garuda.states.game.objects;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.World;

import dav.mobile.garuda.components.Constants;
import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.states.game.PhysicsObject;

public class Garuda extends PhysicsObject {
	//Attributes
	private float 	m_StateTime;
	private Boolean m_Idle;
	
	public Garuda(int x, int y, World m_World) {
		//Super
		super(x, y, ResourceManager.m_GarudaSprite[0], m_World);
		
		//Set Attributes
		m_StateTime = 0;
		m_Idle = true;
	}
	
	//Create physics body
	@Override
	protected void CreatePhysicsBody(World m_World) {
		//Create body definition
		PolygonShape m_GarudaShape = new PolygonShape();
		m_GarudaShape.setAsBox(Constants.GAME_GARUDA_WIDTH / Constants.PIXELS_PER_METER, Constants.GAME_GARUDA_HEIGHT / Constants.PIXELS_PER_METER);
		BodyDef m_GarudaBodyDef = new BodyDef();
		m_GarudaBodyDef.type = BodyType.DynamicBody;
		m_GarudaBodyDef.position.set(getPosX() / Constants.PIXELS_PER_METER, getPosY() / Constants.PIXELS_PER_METER);
		
		//Create body
		m_Body = m_World.createBody(m_GarudaBodyDef);
		
		//Create body fixture
		FixtureDef m_FixtureGaruda = new FixtureDef();
		m_FixtureGaruda.shape = m_GarudaShape;
		m_Body.createFixture(m_FixtureGaruda);
		
		//Dispose shape
		m_GarudaShape.dispose();
	}
	
	//Update
	@Override
	public void Update(float deltatime) {
		//Update time
		m_StateTime += deltatime;

		float vx =  (m_Body.getLinearVelocity().x > 70.0f) 	? 70.0f 	: m_Body.getLinearVelocity().x;
		if (vx<=0) vx = 0;
		
		float vy = 	(m_Body.getLinearVelocity().y > 55.0f) 	? 55.0f		: m_Body.getLinearVelocity().y;
		m_Body.setLinearVelocity(new Vector2(vx, vy));
		
		//System.out.println(m_Body.getLinearVelocity().x + " " + m_Body.getLinearVelocity().y);
		
		//Update position
		m_Position.x += deltatime * m_Body.getLinearVelocity().x * Constants.PIXELS_PER_METER;
		m_Position.y += deltatime * m_Body.getLinearVelocity().y * Constants.PIXELS_PER_METER;
	}
	
	//Render method
	@Override
	public void Render(SpriteBatch m_SpriteBatch) {
		//Initialize
		float m_CenterX, m_CenterY;
			
		//Get current animation frame
		if (m_Idle) {
			m_Texture = ResourceManager.m_GarudaIdle.getKeyFrame(m_StateTime, true);
		} else {
			m_Texture = ResourceManager.m_GarudaAnimation.getKeyFrame(m_StateTime, true);
		}
		
		//Get center position
		m_CenterX = (m_Position.x + m_Texture.getRegionWidth()) / 2;
		m_CenterY = (m_Position.y + m_Texture.getRegionHeight()) / 2;
		
		//draw
		m_SpriteBatch.draw(m_Texture, m_Position.x, m_Position.y, m_CenterX, m_CenterY, m_Texture.getRegionWidth(), m_Texture.getRegionHeight(), 1, 1, 0.0f);
	}
	
	//Fly
	public void Fly() {
		m_Idle = false;
	}
}
